package com.android.place360.place_detail;


import com.android.place360.services.HttpService;
import com.google.gson.Gson;

public class DetailRequest {

	public static String REQUEST_URL = "https://maps.googleapis.com/maps/api/place/details/json?";
	public static String REQUEST_ERROR =  "Lack of required parameters";
	
	private String reference;
	private String language;
	private boolean sensor;
	private String key;
	
	public DetailRequest(String reference, String language, boolean sensor,
			String key) {
		super();
		this.reference = reference;
		this.language = language;
		this.sensor = sensor;
		this.key = key;
	}

	public DetailRequest() {
		super();
	}

	
	public String getRequestString(){
		String request = REQUEST_URL;
		if(!parameterValid(reference)) 	return REQUEST_ERROR;
		if(!parameterValid(sensor)) 	return REQUEST_ERROR;
		if(!parameterValid(key))		return REQUEST_ERROR;
		
		request += "reference=" + reference;
		request += "&sensor="	+ sensor;
		request += "&key="		+ key;
		
		if(parameterValid(language)) 	request += "&language=" + language;
		
		request = request.replace(" ", "%20").trim();
		
		return request;
	}
	
	public String sendRequest(){
		
		String request = getRequestString();
		if(request.equals(REQUEST_ERROR)) return REQUEST_ERROR;
		return HttpService.SendRequest(getRequestString());
		
	}
	
	public DetailResponse getResponse(){
		try {
			String json = sendRequest();
			if(json.equals(REQUEST_ERROR)) return null;
			
			Gson gson = new Gson();
			return gson.fromJson(json, DetailResponse.class);
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	private boolean parameterValid(String para){
		if(para==null || para.trim().equals("")) return false;
		return true;
	}
	private boolean parameterValid(Object para){
		if(para==null) return false;
		return true;
	}
	
	public String getReference() {
		return reference;
	}

	public void setReference(String reference) {
		this.reference = reference;
	}

	public String getLanguage() {
		return language;
	}

	public void setLanguage(String language) {
		this.language = language;
	}

	public boolean isSensor() {
		return sensor;
	}

	public void setSensor(boolean sensor) {
		this.sensor = sensor;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}
	
	
}
